package com.tools.utils.verify;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;

/**
 * @Author LY
 * @create 2023/9/1 15:02
 * @Description 验证码生成控制层
 */
@RestController
public class VerifyCodeController {

    /**
     * 获取验证码表达式
     * @param response
     * @return
     */
    @RequestMapping(value = "/getVerifyCode", method = RequestMethod.GET)
    public Object getVerifyCode(HttpServletResponse response){
        // 创建验证码
        try {
            VerifyCodeVo verifyCode = VerifyCodeUtils.createVerifyCode();
            ServletOutputStream out = response.getOutputStream();
            //将图片写入到resp对象中
            ImageIO.write(verifyCode.getImage(), "JPEG", out);
            out.close();
            out.flush();
            return null;
        }catch (Exception e){
            e.printStackTrace();
            return "创建失败";
        }
    }
}
